import re
import json
import utils
import idmapper
import translator

taming_creatures = utils.read_data("data/scraper/taming-creatures.json")
taming_foods = utils.read_data("data/scraper/taming-foods.json")

def scrape_taming_food(translate=True):
    foods = {}
    for food_name,taming_food in taming_foods.items():
        if food_name == "Kibble":
            for kibble_type in ("Basic","Simple","Regular","Superior","Exceptional","Extraordinary"):
                kibble_name = f"{kibble_type} {food_name}"
                item_id = idmapper.get_id(kibble_name)
                food = {}
                food["itemId"] = item_id
                food["name"] = kibble_name
                food["foodValue"] = taming_food["foodValue"]
                food["affinity"] = taming_food["affinity"]
                foods[item_id] = food
        else:
            food={}
            item_id = idmapper.get_id(food_name)
            food["itemId"] = item_id
            food["name"] = food_name.split(" (",maxsplit=-1)[0]
            food["foodValue"] = taming_food["foodValue"]
            food["affinity"] = taming_food["affinity"]
            foods[item_id] = food

    for creature_name,creature in taming_creatures.items():
        for special_food_name,special_food in creature.get("specialFoodValues",{}).items():
            if special_food_name == "Kibble":
                special_food_name = f"{creature['favoriteKibble']} Kibble"
            item_id = idmapper.get_id(special_food_name)
            creature_id = idmapper.get_id(creature_name)
            food = {
                "itemId":item_id,
                "name":special_food_name.split(" (",maxsplit=-1)[0],
                "foodValue":special_food["value"],
                "affinity":special_food["affinity"]
            }
            foods[f"{item_id}|{food['foodValue']}|{food['affinity']}"]=food
    if translate:
        translator.translate_taming_foods(foods,update_translations=True)
    utils.save_data(foods, "data/taming-foods.json")

def scrape_taming_data():
    taming_data = {}
    for creature_name,creature in taming_creatures.items():
        if "eats" not in creature:
            print("已跳过:",creature_name)
            continue
        creature_id = idmapper.get_id(creature_name)
        # 驯服生物可用的食物
        eats = []
        for food_name in creature["eats"]:
            if food_name == "Kibble":
                food_name = f"{creature['favoriteKibble']} Kibble"
            item_id = idmapper.get_id(food_name)
            if food_name in creature.get("specialFoodValues",()):
                food = creature["specialFoodValues"][food_name]
                item_id = f"{item_id}|{food['value']}|{food['affinity']}"
            eats.append(item_id)
        # 整理后的数据
        data = {
            # 是否为和平驯服
            "isNonViolentTame":creature.get("nonViolentTame")==1,
            # 基础好感度
            "affinityBase":creature["affinityNeeded0"],
            # 每升1级加的好感度
            "affinityIncrease":creature["affinityIncrease"],
            # 清醒时的好感度增长倍率
            "wakeAffinityMult":creature.get("wakeAffinityMult",1),
            # 用于计算每次进食之后下降的驯服有效性
            "ineffectiveness":creature["tamingIneffectiveness"],
            # 驯服时可用的食物
            "eats":eats,
            # 食物消耗速度
            "foodConsumptionBase":creature["foodConsumptionBase"],
            # 食物消耗倍率
            "foodConsumptionMult":creature["foodConsumptionMult"],
            # 清醒时食物的消耗倍率
            "wakeFoodDeplMult":creature.get("wakeFoodDeplMult",1),
            # 是否为重武器击晕
            "isHeavyWeaponKnockout":creature.get("heavyWeaponKnockout")==1
        }
        # 如果是击晕驯服，则还会有和眩晕值相关的数据
        if not data["isNonViolentTame"]:
            # 基础眩晕值
            data["torporBase"]=creature["torpor1"]
            # 每升1级所加的眩晕值上限
            data["torporIncrease"]=creature["torporIncrease"]
            # 眩晕值每秒钟下降的速度
            data["torporDepletionRate"]=creature["torporDepletionPS0"]
        if data["isHeavyWeaponKnockout"]:
            # 每种重武器涨的眩晕值
            data["heavyWeaponTorporValue"]=creature["heavyWeaponTorporValue"]
        taming_data[creature_id] = data
    utils.save_data(taming_data, "data/taming-data.json")
            
